package com.itda.controller;

import com.itda.pojo.Result;
import com.itda.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: itda
 * @Date: 2024/4/28 19:26
 * @Description: UploadController
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传:{},{},{}",username,age,image);
        //获取原始的文件名
        String originalFilename = image.getOriginalFilename();

        //获取文件拓展名
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);

        //构造唯一的文件名(不能重复) --uuid(通用唯一标识符)
        String newFileName = UUID.randomUUID().toString() + extname;
        log.info("新的文件名:{}",newFileName);

        //将文件存储在服务器的磁盘目录中:D:\桌面\image
        image.transferTo(new File("D:/桌面/image/" + newFileName));

        return Result.success();
    }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传:{}", image.getOriginalFilename());

        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成,文件访问的url:{}", url);

        return Result.success(url);
    }
}
